package de.tum.in.eist.im.client.Registration;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.in.eist.im.client.Login.LoginPanel;

public class RegistrationPanel {
	
	// private variables
	final private Label emailLabel = new Label();
	final private Label passwordLabel = new Label();
	final private Label repeatPasswordLabel = new Label();
	final private Label errorLabel = new Label();
	
	final private TextBox emailBox = new TextBox();
	final private PasswordTextBox passwordBox = new PasswordTextBox();
	final private PasswordTextBox repeatPasswordBox = new PasswordTextBox();
	
	final private Button registrationButton = new Button();
	final private Button backButton = new Button();
	
	/**
	 * standard constructor for the registration panel
	 * creates a panel where the user can register himself
	 */
	public RegistrationPanel() {
		// clear panel
		final RootLayoutPanel rootPanel = RootLayoutPanel.get();
		rootPanel.clear();
		
		// create registration panel
		VerticalPanel registrationPanel = new VerticalPanel();
		
		// set labels
		emailLabel.setText("Username:");
		passwordLabel.setText("Password:");
		repeatPasswordLabel.setText("Repeat Password:");
		
		backButton.setText("Back to Login");
		registrationButton.setText("Registration");
		
		// create horizontal panels
		HorizontalPanel emailPanel = new HorizontalPanel();
		HorizontalPanel passwordPanel = new HorizontalPanel();
		HorizontalPanel repeatPasswordPanel = new HorizontalPanel();
		HorizontalPanel buttonPanel = new HorizontalPanel();

		// add widgets to panels
		emailPanel.add(emailLabel);
		emailPanel.add(emailBox);
		
		passwordPanel.add(passwordLabel);
		passwordPanel.add(passwordBox);
		
		repeatPasswordPanel.add(repeatPasswordLabel);
		repeatPasswordPanel.add(repeatPasswordBox);
		
		buttonPanel.add(backButton);
		buttonPanel.add(registrationButton);
		
		registrationPanel.add(emailPanel);
		registrationPanel.add(passwordPanel);
		registrationPanel.add(repeatPasswordPanel);
		registrationPanel.add(buttonPanel);
		registrationPanel.add(errorLabel);
		
		rootPanel.add(registrationPanel);
		
		// add styles
		registrationPanel.addStyleName("centerPanel");
		emailPanel.addStyleName("registrationTextPanel");
		passwordPanel.addStyleName("registrationTextPanel");
		repeatPasswordPanel.addStyleName("registrationTextPanel");
		emailBox.addStyleName("registrationTextbox");
		passwordBox.addStyleName("registrationTextbox");
		repeatPasswordBox.addStyleName("registrationTextbox");
		emailLabel.addStyleName("registrationTextLabel");
		passwordLabel.addStyleName("registrationTextLabel");
		repeatPasswordLabel.addStyleName("registrationTextLabel");
		
		// set handler
		RegistrationHandler registrationHandler = new RegistrationHandler(this);
		registrationButton.addClickHandler(registrationHandler);
		
		backButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				rootPanel.clear();
				new LoginPanel();
			}		
		});
	}
	
	/**
	 * get the name in the user text box
	 * @return the text in the user text box
	 */
	public String getUsername() {
		return this.emailBox.getText();
	}
	
	/**
	 * get the password in the password text box
	 * @return the text in the password text box
	 */
	public String getPassword() {
		return this.passwordBox.getText();
	}
	
	/**
	 * get the repeated password in the repeated password text box
	 * @return the repeated password in the repeated password text box
	 */
	public String getRepeatedPassword() {
		return this.repeatPasswordBox.getText();
	}
	
	/**
	 * set the error label
	 * @param error error which should be displayed
	 */
	public void setErrorLabel(String error) {
		this.errorLabel.setText(error);
	}
}
